package com.cheer.ba.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作者：Administrator on 2016/8/18 0018 17:35
 * 功能：通用的popopwindow
 * 说明：
 */
public class ShowPopupView implements AdapterView.OnItemClickListener, View.OnClickListener {

    //上下文
    private Context mContext;

    private PopupWindow popWindow;

    private LayoutInflater mInflater;

    //内容布局
    private View viewContent;

    private ListView mListView;

    private CommonAdapter<String> mAdapter;

    //存储数据
    private List<String> mItems = new ArrayList<String>();

    //记录点击的下标
    private int mRecordPosition = 0;

    private String mTitle;

    //存储item的颜色
    private int[] mItemColor = null;

    //弹出框初始化
    public ShowPopupView(Context mContext) {
        this.mContext = mContext;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewContent = mInflater.inflate(R.layout.view_popup_window, null);
        mListView = (ListView) viewContent.findViewById(R.id.id_popup_listview);
        viewContent.findViewById(R.id.id_popup_cancel_btn).setOnClickListener(this);
        mAdapter = new CommonAdapter<String>(mContext, mItems, R.layout.comm_textview) {
            @Override
            public void convert(ViewHolder helper, String item) {
                TextView mTxt = (TextView) helper.getViewById(R.id.id_popup_txt);
                mTxt.setText(item);
                if (null != mItemColor) {
                    mTxt.setTextColor(mContext.getResources().getColor(mItemColor[helper.getPosition()]));
                }
            }
        };
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    /**
     * 设置item的颜色
     */
    public void setItemColor(int[] colors) {
        this.mItemColor = colors;
//        mAdapter.notifyDataSetChanged();
    }

    /**
     * 展示popupwindow
     *
     * @param root  展示层
     * @param items 数据
     */
    public void showAtLocPop(View root, String[] items) {
        if (null != popWindow && popWindow.isShowing()) {
            return;
        }
        mItems.clear();
        mItems.addAll(Arrays.asList(items));
        mAdapter.notifyDataSetChanged();

        popWindow = new PopupWindow(viewContent, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popWindow.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popWindow.showAtLocation(root, Gravity.BOTTOM, 0, 0);
    }

    public void showAtLocPopCenter(View root, String[] items) {
        if (null != popWindow && popWindow.isShowing()) {
            return;
        }
        mItems.clear();
        mItems.addAll(Arrays.asList(items));
        mAdapter.notifyDataSetChanged();

        popWindow = new PopupWindow(viewContent, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popWindow.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popWindow.showAtLocation(root, Gravity.CENTER, 0, 0);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            //
            case R.id.id_popup_listview:
                mListener.OnItemClick(i, "");
                popWindow.dismiss();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_popup_cancel_btn:
                popWindow.dismiss();
                break;
        }
    }

    private OnItemPopupClick mListener;

    public interface OnItemPopupClick {
        void OnItemClick(int position, String tag);
    }

    public void setOnItemPopupClick(OnItemPopupClick mListener) {
        this.mListener = mListener;
    }
}
